package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.exception.CatchConsumerException;
import com.usian.common.fastdfsclient.FastDFSClient;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmMaterialDto;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmNewsMaterial;
import com.usian.model.media.pojos.WmUser;
import com.usian.utils.threadlocal.AdminThreadLocalUtils;
import com.usian.utils.threadlocal.WmThreadLocalUtils;
import com.usian.wemedia.mapper.WmMaterialMapper;
import com.usian.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {


    @Value("${wmaterial.baseurl}")
    private String baseUrl;

    @Autowired
    private FastDFSClient fastDFSClient;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Override
    public PageResponseResult findPage() {
        //创建模糊查询条件
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        WmMaterialDto wmMaterialDto = new WmMaterialDto();
        Page<WmMaterial> page = new Page<>(wmMaterialDto.getPage()==null?1:wmMaterialDto.getPage(),
                wmMaterialDto.getSize()==null?10:wmMaterialDto.getSize());
        this.page(page,wrapper);
        //创建返回对象
        PageResponseResult result = new PageResponseResult();
        //为返回对象进行赋值
        result.setCurrentPage(page.getCurrent());
        result.setSize(page.getSize());
        result.setData(page.getRecords());
        result.setTotal(page.getTotal());
        result.setCode(200);
        return result;
    }

    @Override
    public ResponseResult delPicture(Integer id) {
        if(id == null){
            CatchConsumerException.catchException(20003,"id不能为空");
        }
        WmUser wmUser = WmThreadLocalUtils.getUser();
        if(wmUser==null){
            CatchConsumerException.catchException(20003,"用户尚未登录，不能删除！");
        }
        WmMaterial wmMaterial = wmMaterialMapper.selectById(id);
        boolean result = this.remove(Wrappers.<WmMaterial>lambdaQuery().eq(WmMaterial::getUserId,wmUser.getId()).eq(WmMaterial::getId,id));
        if(result){
            // 删除对应 OSS 、KODO 、 FastDfs
            // fileId   =   group1/M00/00/00/wKjThGNOR-KAGneZAAASweNeHFM103.png
            //fastDFSClient.delFile("");
            FastDFSClient fastDFSClient = new FastDFSClient();
            fastDFSClient.delFile(wmMaterial.getUrl());
            this.removeById(id);
        }
        return  ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
       WmUser wmUser = WmThreadLocalUtils.getUser();
        WmMaterial wmMaterial = null;
        if(multipartFile==null){
            CatchConsumerException.catchException(20001,"文件不能为空！");
        }
        String imagesUrl = "";
        try {
            // 阿里云
            //OSSClientUtil ossClientUtil = new OSSClientUtil();
            //imagesUrl =  ossClientUtil.saveImage(multipartFile);
            // 七牛云
            //imagesUrl =  kodoUtils.saveImages(multipartFile);
            //fastDFS
            imagesUrl =  fastDFSClient.uploadFile(multipartFile);
            wmMaterial =  saveUrlByWmMaterial(imagesUrl,wmUser);
            // 5. 把刚才上传的图片路径  返回给前端
            wmMaterial.setUrl(baseUrl + wmMaterial.getUrl());
        }catch (Exception e){
            CatchConsumerException.catchException(20001,"上传图片出现未知错误！");
        }
        return ResponseResult.okResult(wmMaterial);
    }



    private WmMaterial saveUrlByWmMaterial(String imageUrl,WmUser wmUser){
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUrl(imageUrl);
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setUserId(wmUser.getId());
        wmMaterial.setType((short)0);
        this.save(wmMaterial);
        return wmMaterial;
    }


    @Override
    public ResponseResult updateStatus(Integer id, Short type) {
        // 1. 检查参数
        if(id == null){
            CatchConsumerException.catchException(20003,"ID为空，不能操作！");
        }
        if(type >1 || type < 0){
            CatchConsumerException.catchException(20003,"收藏状态有误，不能操作！");
        }
        WmUser wmUser = WmThreadLocalUtils.getUser();
        if(wmUser == null){
            CatchConsumerException.catchException(20003,"用户尚未登录，不能操作！");
        }
        // 调用修改语句  update set  where
        this.update(Wrappers.<WmMaterial>lambdaUpdate()
                .set(WmMaterial::getIsCollection,type)
                .eq(WmMaterial::getId,id)
                .eq(WmMaterial::getUserId,wmUser.getId()));

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
